package cn.yuxiaoyuan.article.controller;

import cn.yuxiaoyuan.annotation.PreAuthorize;
import cn.yuxiaoyuan.article.domain.ArticleType;
import cn.yuxiaoyuan.article.query.ArticleTypeQuery;
import cn.yuxiaoyuan.article.service.ArticleTypeService;
import cn.yuxiaoyuan.constant.RedisConstant;
import cn.yuxiaoyuan.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api("ArticleType相关的接口")
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PreAuthorize(sn = "articleType:findById",name = "文章类型根据id查询",descs = "文章类型根据id查询")
    @GetMapping("/{id}")
    public R findById(@PathVariable("id")Long id){
        return R.ok(articleTypeService.findById(id));
    }

    @PreAuthorize(sn = "articleType:findAll",name = "文章类型查询全部",descs = "文章类型查询全部")
    @GetMapping
    public R findAll(){
        return R.ok(articleTypeService.findAll());
    }

    @PreAuthorize(sn = "articleType:deleteById",name = "文章类型根据id删除",descs = "文章类型根据id删除")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")Long id){
        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        articleTypeService.deleteById(id);
        return R.ok();
    }
    @PreAuthorize(sn = "articleType:batchDelete",name = "文章类型删除全部",descs = "文章类型删除全部")
    @PostMapping("/deleteAll")
    public R batchDelete(@RequestBody List<Long> ids){
        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        articleTypeService.batchDelete(ids);
        return R.ok();
    }

    @PreAuthorize(sn = "articleType:add",name = "文章类型添加和修改",descs = "文章类型添加和修改")
    @ApiOperation("添加和修改")
    @PostMapping
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public R add(@RequestBody ArticleType articleType){
//        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        if(articleType.getId() == null){
            articleType.setCreateTime(new Date());
            articleTypeService.add(articleType);
        }else {
            articleType.setUpdateTime(new Date());
            articleTypeService.update(articleType);
        }
        return R.ok();
    }

    @PreAuthorize(sn = "articleType:getPage",name = "文章类型分页查询",descs = "文章类型分页查询")
    @PostMapping("/page")
    public R getPage(@RequestBody ArticleTypeQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<ArticleType> articleTypes = articleTypeService.queryPage(query);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(articleTypes);
        return R.ok(pageInfo);
    }

    @PreAuthorize(sn = "articleType:articleTypeTree",name = "文章类型树",descs = "文章类型树")
    @GetMapping("/articleTypeTree")
    public R articleTypeTree(){
        return R.ok(articleTypeService.articleTypeTree());
    }
}
